<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\GzdExcelExpoter;
use App\Models\GzdMubanDetail;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class GzdMubanDetailController extends AdminController
{
    protected $title = '模板详情';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GzdMubanDetail(), function (Grid $grid) {

            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->disableDeleteButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('muban_id', '模板')->select(function ($muban_id){
                    return DB::table('gzd_muban')->where('deleted_at',null)->pluck('name as text','id');
                })->width(5);

            });

            if(Admin::user()->sensitive == 0){
                $grid->export(new GzdExcelExpoter());
            }

            $grid->number('序号');
            $grid->column('name');
            $grid->column('quantity','工数');
            $grid->column('type_work','工种');
            $grid->column('basic_salary','合同工值');
            $grid->column('payable','应发');
            $grid->column('actual','借支')->editable(true);
            $grid->column('other','其他')->editable(true);
            $grid->column('advance','实发');
            $grid->column('bank_number','银行卡号');
            $grid->column('bank_deposit','开户银行');

            $grid->setActionClass(Grid\Displayers\ContextMenuActions ::class);

//            $grid->column('muban_id');
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();


            $grid->footer(function ($collection) use ($grid) {
                $muban_id = $grid->model()->filter()->input('muban_id');

                // 查出统计数据
                $current_total = DB::table('gzd_muban_details')->where('muban_id',$muban_id)->sum('advance');

                $target_total = DB::table('gzd_muban')->where('id',$muban_id)->sum('total_amount');

                return "<button class='btn btn-primary btn-light shadow-none' data-toggle='dropdown'>当前总额度:$current_total</button>
                    <button class='btn btn-primary btn-light shadow-none' data-toggle='dropdown'>目标总额度:$target_total</button>";
            });


        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GzdMubanDetail(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('id_card');
            $show->field('engineering');
            $show->field('type_work');
            $show->field('day');
            $show->field('basic_salary');
            $show->field('salary');
            $show->field('muban_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GzdMubanDetail(), function (Form $form) {
            $form->display('id');
//            $form->text('name');
//            $form->text('id_card');
//            $form->text('engineering');
//            $form->text('type_work');
//            $form->text('day');
//            $form->text('basic_salary');
//            $form->text('salary');
//            $form->text('muban_id');
            $form->text('payable');
            $form->text('actual');
            $form->text('other');

            $form->display('created_at');
            $form->display('updated_at');

            $form->saved(function (Form $form, $result) {

                $data = $form->repository()->model();

                $advance = $data->payable - $data->actual - $data->other;

                DB::table('gzd_muban_details')->where('id',$data->id)->update(['advance'=>$advance]);


            });

        });
    }
}
